package com.wanmait.translate.dto;

import java.util.List;

/*
 * @Author huweibin
 * @Description 用于包装返回值的类
 * @Date 13:51 2020/11/7
 **/
public class ResultData <T> {

/**
    {
        "code": 0,
            "msg": "ok",
            "data": [
 **/
        /**
        * @Author huweibin
        * @Date 13:54 2020/11/7
        * @Description
        *  code：状态码，（1 成功 0失败）非必须
        *  msg：携带的消息
        *  T：要返回的数据
        **/
        private Integer code=1;
        private String msg="ok";
        private T data;
        //本次携带的数据量
        private Integer count=0;

        private Integer total=0;
        private ResultData(){};
    public static <T> ResultData SuccesssWithData(T t){
        ResultData resultData = new ResultData();
        resultData.setData(t);
        if (t instanceof List){
            resultData.setCount(((List) t).size());
        }

       return resultData;
    }
    public static <T> ResultData successsWithData(T t,String msg){
        ResultData resultData = new ResultData();
        resultData.setData(t);
        resultData.setMsg(msg);
        if (t instanceof List){
            resultData.setCount(((List) t).size());
        }
        return resultData;
    }
    public static <T> ResultData successsWithData(Integer code,T t,String msg){
        ResultData resultData = new ResultData();
        resultData.setData(t);
        resultData.setMsg(msg);
        resultData.setCode(code);
        if (t instanceof List){
            resultData.setCount(((List) t).size());
        }
        return resultData;
    }
    public static <T> ResultData successsWithData(Integer code,T t){
        ResultData resultData = new ResultData();
        resultData.setData(t);
        resultData.setCode(code);
        if (t instanceof List){
            resultData.setCount(((List) t).size());
        }
        return resultData;
    }
    public static <T> ResultData successsWithOutData(String msg){
        ResultData resultData = new ResultData();
        resultData.setMsg(msg);
        return resultData;
    }
    public static <T> ResultData successsWithOutData(){
        return new ResultData();
    }
    public static <T> ResultData fail(String msg){
        ResultData resultData = new ResultData();
        resultData.setMsg(msg);
        resultData.setCode(0);
        return resultData;
    }
    public static <T> ResultData fail( ){
        ResultData resultData = new ResultData();
        resultData.setMsg("fail");
        resultData.setCode(0);
        return resultData;
    }
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "ResultData{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", count=" + count +
                '}';
    }
}
